{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 19,
   "metadata": {},
   "outputs": [],
   "source": [
    "import random\n",
    "import numpy as np\n",
    "from math import sqrt\n",
    "from collections import defaultdict\n",
    "from time import time"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 20,
   "metadata": {},
   "outputs": [],
   "source": [
    "def ParseDataFromFile(f):\n",
    "    for l in open(f):\n",
    "        yield eval(l)\n",
    "data=list(ParseDataFromFile(\"train.json\"))\n",
    "np.random.shuffle(data) "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 21,
   "metadata": {},
   "outputs": [],
   "source": [
    "def load(data):\n",
    "    for i in data:\n",
    "        UserS.add(i['userID'])\n",
    "        BuzS.add(i['businessID'])\n",
    "\n",
    "    j=0\n",
    "    for i in UserS:\n",
    "        UserS2N[i]=j\n",
    "        UserN2S[j]=i\n",
    "        j+=1\n",
    "\n",
    "    j=0\n",
    "    for i in BuzS:\n",
    "        BuzS2N[i]=j\n",
    "        BuzN2S[j]=i\n",
    "        j+=1\n",
    "UserS   = set()\n",
    "UserS2N = {}\n",
    "UserN2S = {}\n",
    "BuzS    = set()\n",
    "BuzS2N  = {}\n",
    "BuzN2S  = {}\n",
    "load(data)\n",
    "lenu=len(UserS)\n",
    "lenb=len(BuzS)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 22,
   "metadata": {},
   "outputs": [],
   "source": [
    "train=data[:166667]\n",
    "validate=data[166667:]\n",
    "\n",
    "UserBlist_T=defaultdict(list);BuzUlist_T =defaultdict(list)\n",
    "for i in train:\n",
    "    u=UserS2N[i['userID']];b=BuzS2N[i['businessID']]\n",
    "    UserBlist_T[u].append(b);BuzUlist_T[b].append(u)\n",
    "    \n",
    "UserBlist=defaultdict(list);BuzUlist =defaultdict(list)\n",
    "for i in data:\n",
    "    u=UserS2N[i['userID']];b=BuzS2N[i['businessID']]\n",
    "    UserBlist[u].append(b);BuzUlist[b].append(u)\n",
    "    \n",
    "xy_V={}\n",
    "for i in validate:\n",
    "    u=UserS2N[i['userID']];b=BuzS2N[i['businessID']]\n",
    "    xy_V[(u,b)]=1\n",
    "\n",
    "i=0\n",
    "while i<len(validate):\n",
    "    b=int(random.uniform(0,len(BuzS)))\n",
    "    u=int(random.uniform(0,len(UserS)))\n",
    "    if b not in UserBlist[u] and (u,b) not in xy_V:\n",
    "        xy_V[(u,b)]=0;i+=1"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 23,
   "metadata": {},
   "outputs": [],
   "source": [
    "xy={}\n",
    "for i in data:\n",
    "    u=UserS2N[i['userID']];b=BuzS2N[i['businessID']]\n",
    "    xy[(u,b)]=1\n",
    "\n",
    "i=0\n",
    "while i<len(data):\n",
    "    b=int(random.uniform(0,len(BuzS)))\n",
    "    u=int(random.uniform(0,len(UserS)))\n",
    "    if b not in UserBlist[u] and (u,b) not in xy:\n",
    "        xy[(u,b)]=0;i+=1"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 24,
   "metadata": {},
   "outputs": [],
   "source": [
    "Buzt_T={}\n",
    "for i in BuzUlist_T:\n",
    "    Buzt_T[i]=len(BuzUlist_T[i])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 26,
   "metadata": {},
   "outputs": [],
   "source": [
    "class BPR(object):\n",
    "    def __init__(self,trainpairs,testset, F=10, alpha=0.03, lmbd={'u':0.1,'p':0.1,'n':0.1}, max_iter=500):\n",
    "        '''\n",
    "        lmbd是字典{u:1,p:1,n:1}\n",
    "        F维度\n",
    "        trainpairs: u:[i0,i1]\n",
    "        '''\n",
    "        self.F = F\n",
    "        self.gu=np.matrix([[random.random() for i in range(F)] for i in range(lenu)])\n",
    "        self.gi=np.matrix([[random.random() for i in range(F)] for i in range(lenb)])\n",
    "        self.alpha = alpha\n",
    "        self.lmbd = lmbd\n",
    "        self.max_iter = max_iter\n",
    "        self.trainpairs = trainpairs\n",
    "        self.testset=testset\n",
    "        #self.coeffp=0\n",
    "        self.tmp={}\n",
    "    def sample(self):\n",
    "        u=int(random.uniform(0,lenu))\n",
    "        while u not in self.trainpairs:\n",
    "            u=int(random.uniform(0,lenu))\n",
    "        i=np.random.choice(self.trainpairs[u])\n",
    "        j=i\n",
    "        while j in self.trainpairs[u]:\n",
    "            j=int(random.uniform(0,lenb))\n",
    "        return u,i,j\n",
    "    \n",
    "    def update(self,u,i,j):\n",
    "        #assert u in self.trainpairs and isinstance(i,int) and isinstance(j,int),\" sample wrong\"\n",
    "        #assert i!=j and i in self.trainpairs[u] and j not in self.trainpairs[u],\" sample wrong\"\n",
    "        x_ui=float(np.inner(self.gu[u],self.gi[i]))\n",
    "        x_uj=float(np.inner(self.gu[u],self.gi[j]))\n",
    "        x_uij=x_ui-x_uj\n",
    "#         if x_uij>10:\n",
    "#             return\n",
    "        self.tmp['u']=u;self.tmp['i']=i;self.tmp['j']=j;self.tmp['x_ui']=x_ui;self.tmp['x_uj']=x_uj;self.tmp['x_uij']=x_uij#\n",
    "        \n",
    "        #coeff=np.e ** (-x_uij) / (1 + np.e ** (-x_uij))\n",
    "        coeff=1 / (1 + np.e ** (x_uij))\n",
    "        #self.coeffp=coeff\n",
    "        gradu= self.gi[i]-self.gi[j]\n",
    "        nextgu=self.alpha*(coeff*gradu+self.lmbd['u']*self.gu[u])#/Buzt_T[i]##？\n",
    "        gradi= self.gu[u]\n",
    "        nextgi=self.alpha*(coeff*gradi+self.lmbd['p']*self.gi[i])#/Buzt_T[i]##？\n",
    "        gradj=-self.gu[u]\n",
    "        nextgj=self.alpha*(coeff*gradj+self.lmbd['n']*self.gi[j])#/Buzt_T[i]##？\n",
    "        \n",
    "        self.gu[u]+=nextgu\n",
    "        self.gi[i]+=nextgi\n",
    "        self.gi[j]+=nextgj\n",
    "        \n",
    "    def train(self,inputt=-1):\n",
    "        '''随机梯度下降法训练参数gu和gi '''\n",
    "        t=self.max_iter if inputt<1 else inputt\n",
    "        for step in xrange(t):\n",
    "\n",
    "            u,i,j=self.sample()\n",
    "            self.update(u,i,j)\n",
    "        #self.alpha*=0.9   \n",
    "        \n",
    "    \n",
    "    def test1(self):\n",
    "        err=0\n",
    "        for pair in self.testset:\n",
    "            u=pair[0];i=pair[1];l=self.testset[pair]*2-1\n",
    "            err+=np.inner(self.gu[u],self.gi[i])*l\n",
    "            #err+=(tmp+l)%2\n",
    "        #err/=1.0*len(self.testset)\n",
    "        return err"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": 242,
   "metadata": {},
   "outputs": [],
   "source": [
    "model=UserBlist,xy_V,F=20, alpha=0.1, lmbd={'u':-0.1,'p':-0.2,'n':-0.1}, max_iter=10000)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": 253,
   "metadata": {},
   "outputs": [],
   "source": [
    "#tttt=0\n",
    "model.alpha=1"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 281,
   "metadata": {},
   "outputs": [],
   "source": [
    "model.alpha*=0.5"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 282,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "0.015625 \t42309.2288346\n",
      "0.015625 \t42301.2609074\n",
      "decrease, stop\n",
      "0.015625 \t42303.933012\n",
      "0.015625 \t42306.4617968\n",
      "0.015625 \t42299.9863614\n",
      "decrease, stop\n",
      "0.015625 \t42300.4340457\n",
      "0.015625 \t42305.3157069\n",
      "0.015625 \t42307.7146347\n",
      "0.015625 \t42309.2580255\n",
      "0.015625 \t42312.603106\n",
      "0.015625 \t42314.1499198\n",
      "0.015625 \t42308.3922325\n",
      "decrease, stop\n",
      "0.015625 \t42313.7172852\n",
      "0.015625 \t42325.4606974\n",
      "0.015625 \t42338.7827184\n",
      "0.015625 \t42347.8442181\n",
      "0.015625 \t42342.9543548\n",
      "decrease, stop\n",
      "0.015625 \t42335.5222933\n",
      "decrease, stop\n",
      "0.015625 \t42339.8654849\n",
      "0.015625 \t42339.745182\n",
      "decrease, stop\n",
      "0.015625 \t42352.5598476\n",
      "0.015625 \t42363.6494724\n",
      "0.015625 \t42318.8936925\n",
      "decrease, stop\n",
      "0.015625 \t42329.5356164\n",
      "0.015625 \t42344.5513287\n",
      "0.015625 \t42354.1666078\n",
      "0.015625 \t42339.3232473\n",
      "decrease, stop\n",
      "0.015625 \t42346.8072746\n",
      "0.015625 \t42371.0417459\n",
      "0.015625 \t42365.5363056\n",
      "decrease, stop\n"
     ]
    }
   ],
   "source": [
    "for ll in range(10):\n",
    "    \n",
    "    model.train(63333)\n",
    "    tttt=tttt1\n",
    "    \n",
    "    tttt1=float(model.test1())    \n",
    "    print model.alpha,'\\t',tttt1\n",
    "    if tttt1<tttt:\n",
    "        print \"decrease, stop\"\n",
    "        #break\n",
    "\n",
    "for ll in range(10):    \n",
    "    model.train(633333)\n",
    "    tttt=tttt1\n",
    "    \n",
    "    tttt1=float(model.test1())    \n",
    "    print model.alpha,'\\t',tttt1\n",
    "    if tttt1<tttt:\n",
    "        print \"decrease, stop\"\n",
    "        #break\n",
    "\n",
    "for ll in range(10):    \n",
    "    model.train(633333)\n",
    "    tttt=tttt1\n",
    "    \n",
    "    tttt1=float(model.test1())    \n",
    "    print model.alpha,'\\t',tttt1\n",
    "    if tttt1<tttt:\n",
    "        print \"decrease, stop\"\n",
    "        #break"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 283,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "0.9188275\n"
     ]
    }
   ],
   "source": [
    "testacu()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": 287,
   "metadata": {},
   "outputs": [],
   "source": [
    "def distance1(ds,b1,b2):#????\n",
    "    set1=set(ds[b1])\n",
    "    set2=set(ds[b2])\n",
    "    try:\n",
    "        return len(set1 & set2)*1.0/ sqrt(len(set1)) /sqrt(len(set2))\n",
    "    except:\n",
    "        return 0"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 298,
   "metadata": {},
   "outputs": [],
   "source": [
    "d0t=[];dlfm=[]\n",
    "#d1t=[]\n",
    "yt =[]\n",
    "for i in xy_V:\n",
    "    dd0=0;dd1=0;dd2=0\n",
    "    for b2 in UserBlist_T[i[0]]:\n",
    "        dd0+=distance1(BuzUlist_T,b2,i[1])\n",
    "    tmp=float(np.inner(model.gu[i[0]],model.gi[i[1]]))\n",
    "    d0t.append(dd0)#user距离\n",
    "    dlfm.append(tmp)\n",
    "    #d1t.append(dd0*1.0/pu[i[0]])#user距离除以买的东西数量\n",
    "    yt.append(xy_V[i])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 302,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "0.63870512412\n"
     ]
    }
   ],
   "source": [
    "findmid=[]\n",
    "for (u,b) in xy_V:\n",
    "    findmid.append(float(np.inner(model.gu[u],model.gi[b])))\n",
    "findmid.sort()\n",
    "midian=findmid[33333];print midian#(40000-63)/2#-1;"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 304,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "0.920109201092\n"
     ]
    }
   ],
   "source": [
    "acc=0\n",
    "for i in range(len(dlfm)):\n",
    "    if dlfm[i]<midian and yt[i]==0:\n",
    "        acc+=1\n",
    "\n",
    "    elif dlfm[i]>=midian and yt[i]==1:\n",
    "        acc+=1\n",
    "print acc*1.0/(len(dlfm))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 300,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "1 \t0.605196051961\n",
      "10 \t0.827618276183\n",
      "30 \t0.850973509735\n",
      "40 \t0.851858518585\n",
      "41 \t0.851963519635\n",
      "42 \t0.852023520235\n",
      "43 \t0.852083520835\n",
      "50 \t0.852158521585\n",
      "60 \t0.852188521885\n",
      "100 \t0.852203522035\n",
      "1000 \t0.852203522035\n"
     ]
    }
   ],
   "source": [
    "for amp in [1,10,30,40,41,42,43,50,60,100,1000]:\n",
    "    out=1- sum([((d0t[i]*amp>1)+yt[i])%2 for i in range(len(d0t))])*1.0/len(d0t)\n",
    "    print amp,'\\t',out"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 305,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "1 \t0.928194281943\n",
      "10 \t0.936189361894\n",
      "30 \t0.930969309693\n",
      "40 \t0.930384303843\n",
      "41 \t0.930384303843\n",
      "42 \t0.930384303843\n",
      "43 \t0.930399303993\n",
      "50 \t0.930339303393\n",
      "60 \t0.930249302493\n",
      "100 \t0.930189301893\n",
      "1000 \t0.930189301893\n"
     ]
    }
   ],
   "source": [
    "for amp in [1,10,30,40,41,42,43,50,60,100,1000]:\n",
    "    out=1- sum([((d0t[i]*amp>1 or dlfm[i]>midian)+yt[i])%2 for i in range(len(d0t))])*1.0/len(d0t)\n",
    "    print amp,'\\t',out"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": 252,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{'i': 7345,\n",
       " 'j': 4494,\n",
       " 'u': 1070,\n",
       " 'x_ui': 1.4449946160492417,\n",
       " 'x_uij': 1.9953248916334951,\n",
       " 'x_uj': -0.5503302755842535}"
      ]
     },
     "execution_count": 252,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "model.tmp"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 164,
   "metadata": {},
   "outputs": [],
   "source": [
    "#model.gu[model.tmp['u']],model.gi[model.tmp['i']]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 257,
   "metadata": {},
   "outputs": [],
   "source": [
    "def testacu():\n",
    "    a=[[],[]]\n",
    "    for pair in xy:\n",
    "        u=pair[0];i=pair[1];\n",
    "        a[xy[pair]].append(float(np.inner(model.gu[u],model.gi[i])))\n",
    "\n",
    "    a01=[]\n",
    "    for i in a[0]:\n",
    "        a01.append(i)\n",
    "    for i in a[1]:\n",
    "        a01.append(i)\n",
    "    a01.sort()\n",
    "\n",
    "    acc=0\n",
    "    for i in a[0]:\n",
    "        if i<a01[len(a[0])]:\n",
    "            acc+=1\n",
    "    for i in a[1]:\n",
    "        if i>a01[len(a[0])]:\n",
    "            acc+=1\n",
    "    print acc*1.0/(len(a[0])+len(a[1]))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "#0.9169075 "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 267,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "(1.1585785151980366, 31.540506955866753, 200000, 200000)"
      ]
     },
     "execution_count": 267,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "sum(a[0])/len(a[0]),sum(a[1])/len(a[1]),len(a[0]),len(a[1])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 347,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "0.509728664961\n"
     ]
    }
   ],
   "source": [
    "findmid=[]\n",
    "for (u,b) in test:\n",
    "    findmid.append(float(np.inner(model.gu[u],model.gi[b])))\n",
    "\n",
    "findmid.sort()\n",
    "midian=findmid[19968+432];print midian#(40000-63)/2#-1;#19968"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 348,
   "metadata": {},
   "outputs": [],
   "source": [
    "# for i in range(len(findmid)):\n",
    "#     if 0.457975016921<findmid[i]<=0.457975016922:\n",
    "#         print i"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 321,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "0\n",
      "17473\n",
      "17458\n"
     ]
    }
   ],
   "source": [
    "predictions = open(\"predictions_Visiting.txt\", 'w')\n",
    "#noUorB=0\n",
    "#findone=0\n",
    "one_c=0\n",
    "zero_c=0;cdd0=0\n",
    "findmid=[]\n",
    "count=0\n",
    "for l in open(\"pairs_Visit.txt\"):\n",
    "    if l.startswith(\"userID\"):\n",
    "        #header\n",
    "        predictions.write(l)\n",
    "        continue\n",
    "    u,b = l.strip().split('-')\n",
    "    predictions.write(u + '-' + b + ',')\n",
    "    \n",
    "    count+=1\n",
    "    if count <5000:###############################\n",
    "        predictions.write(str(0)+ '\\n')\n",
    "        continue\n",
    "    \n",
    "    if u not in UserS2N or b not in BuzS2N:\n",
    "        #noUorB+=1\n",
    "        predictions.write(str(0) + '\\n')        \n",
    "    else:\n",
    "        u = UserS2N[u]\n",
    "        b = BuzS2N[b]\n",
    "        if b in UserBlist[u]:#11\n",
    "            #print \"fuck\"\n",
    "            predictions.write('1'+'\\n')\n",
    "            continue\n",
    "            \n",
    "            \n",
    "        flag = 0\n",
    "        p=float(np.inner(model.gu[u],model.gi[b]))\n",
    "        if p>midian :\n",
    "        #if p>midian:\n",
    "            flag=1\n",
    "            one_c+=1\n",
    "        if p<=midian:\n",
    "            flag=0\n",
    "            zero_c+=1\n",
    "        \n",
    "        predictions.write(str(flag) + '\\n')\n",
    "print cdd0\n",
    "print one_c\n",
    "print zero_c\n",
    "predictions.close()\n",
    "#print noUorB#63"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": 447,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "0.541991444599\n",
      "-0.264852650889\n",
      "0.948936078318\n"
     ]
    }
   ],
   "source": [
    "findmid=[]\n",
    "for (u,b) in test:\n",
    "    findmid.append(float(np.inner(model.gu[u],model.gi[b])))\n",
    "\n",
    "findmid.sort()\n",
    "midian=findmid[19968+720];print midian#(40000-63)/2#-1;\n",
    "low=findmid[5000];print low\n",
    "high=findmid[25000];print high"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 448,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "1546\n",
      "20783\n",
      "20689\n"
     ]
    }
   ],
   "source": [
    "predictions = open(\"predictions_Visiting.txt\", 'w')\n",
    "#noUorB=0\n",
    "#findone=0\n",
    "one_c=0\n",
    "zero_c=0;cdd0=0\n",
    "findmid=[]\n",
    "for l in open(\"pairs_Visit.txt\"):\n",
    "    if l.startswith(\"userID\"):\n",
    "        #header\n",
    "        predictions.write(l)\n",
    "        continue\n",
    "    u,b = l.strip().split('-')\n",
    "    predictions.write(u + '-' + b + ',')\n",
    "    \n",
    "    \n",
    "    if u not in UserS2N or b not in BuzS2N:\n",
    "        #noUorB+=1\n",
    "        predictions.write(str(0) + '\\n')        \n",
    "    else:\n",
    "        u = UserS2N[u]\n",
    "        b = BuzS2N[b]\n",
    "        if b in UserBlist[u]:#11\n",
    "            #print \"fuck\"\n",
    "            predictions.write('1'+'\\n')\n",
    "            continue\n",
    "        flag = 0\n",
    "        \n",
    "        \n",
    "        dd0=0\n",
    "        for b2 in UserBlist[u]:\n",
    "            dd0+=distance1(BuzUlist,b2,b)\n",
    "        \n",
    "        \n",
    "        p=float(np.inner(model.gu[u],model.gi[b]))\n",
    "        \n",
    "        if p<=midian:\n",
    "            #if dd0*9>1:\n",
    "            #    cdd0+=1\n",
    "            flag=0\n",
    "            zero_c+=1\n",
    "        if midian>=p>low and dd0*9>1:\n",
    "            cdd0+=1\n",
    "            flag=1\n",
    "            one_c+=1\n",
    "        if p>midian:\n",
    "        #if p>midian or dd0*9>1:\n",
    "            flag=1\n",
    "            one_c+=1\n",
    "#             if len(BuzUlist[b])<3 and len(UserBlist[u])<3 and p<high:\n",
    "#                 cdd0+=1\n",
    "#                 #print p\n",
    "#                 flag=0\n",
    "#                 one_c-=1\n",
    "#                 zero_c+=1\n",
    "        predictions.write(str(flag) + '\\n')\n",
    "print cdd0\n",
    "print one_c\n",
    "print zero_c\n",
    "predictions.close()\n",
    "#print noUorB#63"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "high u 1 \n",
    "25000->28 18268.0 没用\n",
    "    i 1\n",
    "25000->41 18259.0 没用\n",
    "\n",
    "\n",
    "\n",
    "low\n",
    "5000->1554 18277.0\n",
    "8000->1524  18273\n",
    "10000->1480 18269.0\n",
    "12000->1426 18265.0"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 434,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "18259.0"
      ]
     },
     "execution_count": 434,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "0.91295*2e4"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## build test"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "noUorB=0\n",
    "findone=0\n",
    "test=[]\n",
    "for l in open(\"pairs_Visit.txt\"):\n",
    "    if l.startswith(\"userID\"):\n",
    "        continue\n",
    "    u,b = l.strip().split('-')\n",
    "    if u not in UserS2N or b not in BuzS2N:\n",
    "        noUorB+=1\n",
    "        continue     \n",
    "    else:\n",
    "        u = UserS2N[u]\n",
    "        b = BuzS2N[b]\n",
    "        if b in UserBlist[u]:\n",
    "            #print \"fuck\"\n",
    "            findone+=1\n",
    "            test.append((u,b))\n",
    "            continue\n",
    "        test.append((u,b))\n",
    "print findone\n",
    "#print noUorB#63"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": 392,
   "metadata": {},
   "outputs": [],
   "source": [
    "?\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "print model.alpha\n",
    "model.tmp"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 636,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "(matrix([[ -8.89192839,  -0.73151304,  -4.50616416,   0.89096642,\n",
       "            6.50655107,  -7.09088872,  -4.24004883,   6.28662233,\n",
       "            3.01824697,  -6.74725937,  -0.54622962,  -3.18068238,\n",
       "          -17.73218477,  -3.07281945,  -7.04074195,   3.06614387,\n",
       "            6.0312796 ,  -3.59311379,   0.86464865,   3.70170105]]),\n",
       " matrix([[ -8.81440162,   0.55277196,  -4.1964162 ,   1.34371342,\n",
       "            5.9010913 ,  -7.09807532,  -7.71433164,   5.58618331,\n",
       "            3.25524975,  -6.19698867,  -1.5872698 ,  -2.2408341 ,\n",
       "          -16.17759318,  -1.44255571,  -6.83886146,   2.95858293,\n",
       "            7.62228204,  -2.17230431,  -0.68162792,   3.3671223 ]]))"
      ]
     },
     "execution_count": 636,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "model.gu[model.tmp['u']],model.gi[model.tmp['i']]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 634,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{'i': 1527,\n",
       " 'j': 1025,\n",
       " 'u': 6098,\n",
       " 'x_ui': 728.4376729783028,\n",
       " 'x_uij': 764.9531973130106,\n",
       " 'x_uj': -36.515524334707806}"
      ]
     },
     "execution_count": 634,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": 506,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "[8845, 11593]"
      ]
     },
     "execution_count": 506,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "BuzUlist[14246]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 309,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "13998\n",
      "7.83946378175\n"
     ]
    }
   ],
   "source": [
    "c=0\n",
    "for i in BuzUlist_T:\n",
    "    c+=len(BuzUlist_T[i])\n",
    "    if len(BuzUlist_T[i])>160:\n",
    "        print i\n",
    "print c*1.0/len(BuzUlist_T)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 304,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "21260"
      ]
     },
     "execution_count": 304,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "len(BuzUlist_T)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 305,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "18751"
      ]
     },
     "execution_count": 305,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "len(UserBlist_T)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 639,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "matrix([[-0.06322599,  0.28266666,  0.173661  ,  0.11295225,  0.08433847,\n",
       "          0.17080739, -0.28935086,  0.13841665,  0.23825169, -0.26486144],\n",
       "        [ 0.09578499,  0.17602835, -0.22739015,  0.07135965, -0.17681224,\n",
       "          0.01254262,  0.08640948,  0.14579062, -0.06488709, -0.20864786],\n",
       "        [-0.07856159, -0.20942394,  0.00287907,  0.18783359, -0.30029122,\n",
       "          0.28321223, -0.21056918,  0.06507283,  0.19593247, -0.01972849]])"
      ]
     },
     "execution_count": 639,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "np.matrix([[(-1+2*random.random())/sqrt(10) for i in range(10)] for i in range(3)])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 2",
   "language": "python",
   "name": "python2"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 2
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython2",
   "version": "2.7.13"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
